Micron Document
Livres et Wikis | Archives | Info


Java syntax
part 23/36 Β· 136.0 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
instance or java.lang.Class if the method is static.
β€’ Access modifiers - Identical to those used with classes.

Final methods

A final method cannot be overridden or hidden by subclasses.cite-ref-5[5] This is
used to prevent unexpected behavior from a subclass altering a method
that may be crucial to the function or consistency of the class.cite-ref-6[6]

Example:

public class Base
{
public void m1() {...}
public final void m2() {...}
public static void m3() {...}
public static final void m4() {...}
}
public class Derived extends Base
{
public void m1() {...} // OK, overriding Base#m1()
public void m2() {...} // forbidden
public static void m3() {...} // OK, hiding Base#m3()
public static void m4() {...} // forbidden
}

A common misconception is that declaring a method as final improves
efficiency by allowing the compiler to directly insert the method
wherever it is called (see inline expansion). Because the method is
loaded at runtime, compilers are unable to do this. Only the runtime
environment and JIT compiler know exactly which classes have been
loaded, and so only they are able to make decisions about when to
inline, whether or not the method is final.cite-ref-7[7]

Machine code compilers that generate directly executable,
platform-specific

machine code

, are an exception. When using

static linking

, the compiler can safely assume that methods and variables computable
at

compile-time

may be inlined.

Varargs

This language feature was introduced in J2SE 5.0. The last argument of
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────